#include "device_resource.h"
#include "FSMC_SRAM.h"
#include "ParameterStoreDPT.h"
#include <stdint.h>

const char Flash_Store_version[] __attribute__((at(VESION_TAGE_STORE_ADDRESS))) = {
    'D', 'G', 'U', '-', '4', '0', '7', '-', 'A', 'P', 'P',
    '_', '_', '_', '_', '_', '_', '_', '_',
    'V', NUM_TO_CHAR(FIRMWARE_VERSION_H), '.', NUM_TO_CHAR(FIRMWARE_VERSION_M), '.', NUM_TO_CHAR(FIRMWARE_VERSION_L),
    '_',
    __DATE__[9],
    __DATE__[10],
    MONTH_H,
    MONTH_L,
    DAY_H,
    DAY_L,
    ' ',
    __TIME__[0], __TIME__[1], __TIME__[2], __TIME__[3], __TIME__[4], __TIME__[5], __TIME__[6], __TIME__[7]};

uint32_t ERROR_CODE;
uint8_t LWIP_MAC[6];
struct ParameterStoreDPT *setting_para = (struct ParameterStoreDPT *)PARA_SETT_BEG_ADDR;

/**
 * @brief 将原始字节转换为浮点数，并四舍五入到指定小数位
 * @param data      输入字节流（大端）
 * @param len       字节数：4 或 8
 * @param decimal   小数位数：2 或 4
 * @return          四舍五入后的 float 值（如 48.13 而不是 48.129997）
 */
float bytes_to_decimal_float(const uint8_t *data, int len, int decimal)
{
  if (data == NULL || (len != 4 && len != 8) || (decimal != 2 && decimal != 4))
  {
    return 0.0f;
  }

  int64_t raw_value = 0;

  // 大端解析
  if (len == 4)
  {
    raw_value = (int32_t)(((uint32_t)data[0] << 24) |
                          ((uint32_t)data[1] << 16) |
                          ((uint32_t)data[2] << 8) |
                          ((uint32_t)data[3]));
  }
  else
  { // len == 8
    raw_value = ((int64_t)data[0] << 56) |
                ((int64_t)data[1] << 48) |
                ((int64_t)data[2] << 40) |
                ((int64_t)data[3] << 32) |
                ((int64_t)data[4] << 24) |
                ((int64_t)data[5] << 16) |
                ((int64_t)data[6] << 8) |
                ((int64_t)data[7]);
  }

  float scale = (decimal == 2) ? 0.01f : 0.0001f;
  float value = (float)raw_value * scale;

  // === 关键：四舍五入到指定小数位 ===
  int multiplier;
  if (decimal == 2)
  {
    multiplier = 100; // 保留2位小数
  }
  else
  {                     // decimal == 4
    multiplier = 10000; // 保留4位小数
  }
  return (float)((int)(value * multiplier + 0.5f)) / multiplier;
}
time_t time(time_t *t)
{
  time_t sec; // = rtc_get_seconds(); // 返回 1970 起秒数
  device_time_tickets = device_time_ms_tickets / 1000;
  if (t)
    *t = sec;
  return sec;
}
/* 使用 */
void SoftReset(void)
{
  NVIC_SystemReset();
}

uint8_t enableManualControl;
uint8_t lightning_strike_feedback_signal;

StaticQueue_t xCtrlUnitStaticQueue; // 队列结构体
QueueHandle_t xCtrlUnitQueueHandle; // 声明队列句柄

StaticQueue_t xPublishTopicMsgQueue;       // 队列结构体
QueueHandle_t xPublishTopicMsgQueueHandle; // 声明队列句柄

time_t device_time_tickets;
uint64_t device_time_ms_tickets;

char *cli_output_buffer = (char *)CLI_OUTPUT_BEG_ADDR;

uint8_t g_sfp_setting_status;
int g_sfp_module_status;
rtk_sds_mode_t g_pSdsMode;
uint8_t SFP_present;

uint8_t g_vlan_set_status;

NetworkConfig *net_config = (NetworkConfig *)NET_ETH_SETT_BEG_ADDR;

char *msg_v4 = "IPv4 mode";
char *msg_v6 = "IPv6 mode";
char *msg_dual = "Dual IP mode";


uint8_t w6100_net_status;
uint8_t lwip_net_status;
uint8_t LOG_net_status;
char *subscribe_topic = (char *)MQTT_SUBSCRIBE_TOPIC;

MQTT_PUBLISH_MSG *mqtt_publish_msg_push_to_queue = (MQTT_PUBLISH_MSG *)MQTT_PUB_MSG_SWAP_PUSH_TO_QUEUE;
MQTT_PUBLISH_MSG *mqtt_publish_msg_push_to_server = (MQTT_PUBLISH_MSG *)MQTT_PUB_MSG_SWAP_PUSH_TO_SERVER;
